package com.lancoo.edu.platform.operation.commons.service.bo;

import cn.hutool.core.bean.BeanUtil;
import com.lancoo.edu.platform.operation.commons.domain.api.system.ProductInfo;
import com.lancoo.edu.platform.operation.commons.domain.api.system.ServerInfo;
import com.lancoo.edu.platform.operation.commons.domain.api.system.SystemStatusApi;
import com.lancoo.edu.platform.operation.commons.service.api.YunBaseApiService;
import com.lancoo.edu.platform.operation.commons.config.LancooConfig;
import com.lancoo.edu.platform.operation.commons.domain.vo.SystemInfoVo;
import com.lancoo.edu.platform.operation.commons.exception.ServiceException;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * 系统信息【响应】业务逻辑
 * @author pky
 */
@Service
@AllArgsConstructor
public class SystemInfoBoService {

    private final YunBaseApiService yunBaseApiService;
    private final LancooConfig lancooConfig;

    /**
     * 获取系统信息
     * @return 系统信息
     */
    public SystemInfoVo getSystemInfo() {
        // 获取基础平台配置信息
        ProductInfo productInfo = yunBaseApiService.getProductInfo();
        Optional.ofNullable(productInfo).orElseThrow(() -> new ServiceException("从基础平台获取产品信息失败"));
        SystemInfoVo systemInfoVo = BeanUtil.copyProperties(productInfo, SystemInfoVo.class);
        systemInfoVo.setClientDownUrl(productInfo.getClientDownUrl());
        ServerInfo serverInfo = yunBaseApiService.getOneServerInfo(LancooConfig.SYS_ID);
        systemInfoVo.setItemUrl(Optional.ofNullable(serverInfo.getWebSvrAddr()).orElse(lancooConfig.getServiceUrl()));
        BeanUtils.copyProperties(productInfo, systemInfoVo);
        systemInfoVo.setWebName(systemInfoVo.getProductName());
        systemInfoVo.setIsVersion(productInfo.getIsVersion());
        systemInfoVo.setProductUseRange(productInfo.getProductUseRange());
        return systemInfoVo;
    }

    /**
     * 获取子系统列表
     * @param sysIds 指定子系统ID串，用英文逗号隔开
     * @return 子系统列表
     */
    public List<ServerInfo> getSystemList(String sysIds) {
        return yunBaseApiService.getServerInfoList(sysIds);
    }

    /**
     * 获取子系统状态
     *
     * @param sysIds 制度子系统ID串，用英文逗号隔开
     * @return 子系统状态列表
     */
    public List<SystemStatusApi> getSystemState(String sysIds){
        return yunBaseApiService.getSysState(sysIds);
    }
}
